﻿using com.iot.core.boot;
using com.iot.core.exception;
using com.iot.ht.Controllers.htsoft.data;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion.Internal;
using System.Diagnostics;

namespace com.iot.ht.Controllers.htsoft.service
{
    public class AppStartExe : AppStart
    {
        private Process pro { get;   set; }
        public override async Task<MyApiResult> Start()
        {
            var r = new MyApiResult();
            try
            {

                var exeName = new FileInfo(HtSoftApp.StartInfo).Name;
                exeName=exeName.Substring(0,exeName.LastIndexOf("."));  
                var procArr=Process.GetProcesses();
                foreach(var p in procArr) { 
                    if(p.ProcessName == exeName)
                    {
                        pro = p;
                        return r;//程序已经运行，要求程序名称必须唯一
                    }
                }

                pro = new Process(); 
                pro.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; //把窗口隐藏，使其在后台运行

                pro.StartInfo.FileName = HtSoftApp.StartInfo;  //设置要打开的exe程序的路径
               
                pro.StartInfo.UseShellExecute = true;

                pro.StartInfo.Arguments = HtSoftApp.StartArguments;
                var fi = new FileInfo(HtSoftApp.StartInfo);
                pro.StartInfo.WorkingDirectory = fi.Directory.FullName;
                if (!string.IsNullOrEmpty(HtSoftApp.StartDir))
                {
                    pro.StartInfo.WorkingDirectory = HtSoftApp.StartDir;                                 
                }
                                                
                pro.Start();
               
            }
            catch (Exception ex)
            {
                r.Code = 1;
                r.Msg = MyExceptionUtil.GetExMsg(ex);
            }
            return r;
        }
        public override async Task<MyApiResult> Stop()
        {
            var r = new MyApiResult();
            try
            {
                if (pro == null)
                {
                    return r;
                } 
                pro.Dispose(); 
            }
            catch (Exception ex)
            {
                r.Code = 1;
                r.Msg = MyExceptionUtil.GetExMsg(ex);
            }
            return r;
        }
        public override async Task<MyApiResult> StopEx()
        {
            var r = new MyApiResult();
            try
            {
                if (pro == null)
                {
                    return r;
                }
                pro.Kill(true);
                pro.Dispose();
                pro = null; 
            }
            catch (Exception ex)
            {
                r.Code = 1;
                r.Msg = MyExceptionUtil.GetExMsg(ex);
            }
            return r;
        }

        public override async Task<MyApiResult> Check()
        {
            var r = new MyApiResult();
            try
            {
                if (pro == null)
                {
                    return r;
                }
                pro.Refresh();
                if (pro.HasExited)
                {
                    r.Code = 2;//重启
                }
                
                return r;
            }
            catch (Exception ex)
            {
                r.Code = 1;
                r.Msg = MyExceptionUtil.GetExMsg(ex);
            }
            return r;
        }

       
    }
}
